﻿using System;
using System.Text;
using SecretLabs.NETMF.Hardware;
using Netduino.Sensor.Unit;

namespace Netduino.Sensor
{
    /// <summary>
    /// A class that emulates a temperature sensor without actually using a real physical sensor device.
    /// </summary>
    internal class FakedTemperatureSensor: ISensor
    {
        /// <summary>
        /// The analog input to use for this sensor
        /// </summary>
        private AnalogInput analogInput;

        /// <summary>
        /// Create a new instance with the specified input for capturing values.
        /// </summary>
        /// <param name="analogInput"></param>
        public FakedTemperatureSensor(SecretLabs.NETMF.Hardware.AnalogInput analogInput)
        {            
            this.analogInput = analogInput;
        }

        /// <summary>
        /// Takes a sample from the analog input.
        /// </summary>
        /// <returns></returns>
        public ISample Take()
        {
            return new Sample(
                (double)analogInput.Read() / 500 + 20, //emulate indoor temperature range
                _degreesCelsius);
        }

        /// <summary>
        /// The unit for this sensor.
        /// </summary>
        /// <devdoc>
        /// //degree celsius symbol in unicode with trailing C, to conform to JSON requirements. However, there would also be single character for degrees celsius, see http://en.wikipedia.org/wiki/Celsius#Unicode_character However this did not work on firefox.
        /// </devdoc>
        private IUnit _degreesCelsius = new SpecifiedUnit("degrees celsius", @"\u00B0C"); 


        /// <summary>
        /// Short description what the sensor's data represents.
        /// </summary>
        /// <remarks>Example: "Indoor temperature"</remarks>
        public string Caption
        {
            get
            {
                return "Indoor temperature";
            }    
        }
    }
}
